package com.hsj.mqconsumer.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author:Teacher黄
 * @date:Created at 2020/01/04
 */
@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "hsj_queue",durable = "true",exclusive ="false",autoDelete = "false"),
        exchange = @Exchange(name = "hsj_exchange"),
        key = "hsj_route"
))
public class RabbitConsumer {


    @RabbitHandler
    public void doConsume(String msg, @Headers Map<String,Object> headers, Channel channel) throws IOException {
        System.out.println("接收到了消息:"+msg);

        // 获取 deliverTag: 消息的消费id
        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deliverTag);
        // 手动ack
        channel.basicAck(deliverTag,true);
    }

}
